package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.ExchangeRecordAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.ExchangeRecord;
import com.haotunet.app.goldtreasure.model.ExchangeRecordModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

public class ExchangeRecordFragment extends BaseFragment {
    private View view;
    private PullableListView mLvExchangeRecord;
    private PullToRefreshLayout mfreshLayout;
    private LinearLayout mNoRecord;
    private boolean isPrepared;
    private boolean mHasLoadedOnce;
    private int pageNum = 20;
    private final int GET_EXCHANGE_RECORD_SUCCESS = 2;
    private final int LOAD_MORE_EXCHANGE_RECORD_SUCCESS = 3;
    private Handler handle = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case GET_EXCHANGE_RECORD_SUCCESS:
                    ExchangeRecordModel model = (ExchangeRecordModel) msg.obj;
                    List<ExchangeRecord> exchangetList = model.getItems();
                    if (exchangetList.size() == 0) {
                        mNoRecord.setVisibility(View.VISIBLE);
                        return;
                    }
                    exchangeRecordAdapter = new ExchangeRecordAdapter(getActivity(), exchangetList);
                    mLvExchangeRecord.setAdapter(exchangeRecordAdapter);
                    break;
                case LOAD_MORE_EXCHANGE_RECORD_SUCCESS:
                    ExchangeRecordModel moreModel = (ExchangeRecordModel) msg.obj;
                    if (moreModel.getItems().size() == 0) {
                        UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
                    }
                    exchangeRecordAdapter.addDatas(moreModel.getItems());
                    break;
                default:
                    break;
            }
        }

    };

    private ExchangeRecordAdapter exchangeRecordAdapter;

    public static ExchangeRecordFragment newInstance(String type, int index) {
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putInt("index", index);
        ExchangeRecordFragment fragment = new ExchangeRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_exchange_record, null);
        initView();
        isPrepared = true;
        if ("exchangeSuccess".equals(getArguments().getString("type"))|| getArguments().getInt("index", 0) == 4){
            getExchangeGoldRecord("0", "");
        }
        return view;
    }

    private void getExchangeGoldRecord(String pagenum, final String action) {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getExchangeRecord(pagenum);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                ExchangeRecordModel exchangeModel = (ExchangeRecordModel) model;
                if (exchangeModel == null) {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    mfreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    return;
                }
                if (exchangeModel.getErrorMsg() != null) {
                    if (exchangeModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(getActivity(), getActivity().getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(getActivity(), exchangeModel.getErrorMsg());
                    }

                } else {
                    mHasLoadedOnce = true;
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_EXCHANGE_RECORD_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_EXCHANGE_RECORD_SUCCESS;
                    }
                    msg.obj = exchangeModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        mNoRecord = (LinearLayout) view.findViewById(R.id.no_trade_record_layout);
        mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
        mLvExchangeRecord = (PullableListView) view.findViewById(R.id.lv_exchange_record);
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getExchangeGoldRecord(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                getExchangeGoldRecord(pageNum + "", "loadmore");
            }
        });
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        getExchangeGoldRecord("0", "");
    }
}
